package ylsb.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ylsb.service.HaocaiyinjinService;
import ylsb.service.TiwaizhenduanService;
import ylsb.support.JsonResult;
import ylsb.support.PageInfo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tiwaizhenduan")
public class TiwaizhenduanController {
    @Resource
    TiwaizhenduanService ddService;

    public Logger log = LoggerFactory.getLogger(YltpBaofeiController.class);


    @RequestMapping("findtiwaizhenduanlist")
    public JsonResult<?> findTiwaizhenduanlist(@RequestParam("xiangmuid") String ppxiangmuId,@RequestParam("pageindex") int ppPageIndex,
                                              @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmTiwaiList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddService.findPinzhongAll(ppxiangmuId,ppPageIndex,ppPageSize);
            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    String mmPinzhongOne = mmMap.get("pinzhong") == null ? "" : mmMap.get("pinzhong").toString();
                    List<Map<String,Object>> mmPinpailist = ddService.findPinpaiByPinzhong(mmPinzhongOne);
                    if (mmPinpailist.size()>0){
                        mmPinzhongHebingshu = mmPinpailist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    mmMap.put("pinpailist",mmPinpailist);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmTiwaiList.add(mmMap);
            }
            Integer mmCount = ddService.getTiwaizhenduanlist(ppxiangmuId,ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("TiwaizhenduanList", mmTiwaiList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("tiwaizhenduan/findtiwaizhenduanlist:error",e);
            return JsonResult.getErrorResult("tiwaizhenduan/findtiwaizhenduanlist:error " + e.getMessage());
        }

    }

    @RequestMapping("findBiaojuelist")
    public JsonResult<?> findBiaojuelist(@RequestParam("xiangmuid") String ppxiangmuId,
                                         @RequestParam("userid") String ppuserId,
                                         @RequestParam("pageindex") int ppPageIndex,
                                         @RequestParam("pagesize") int ppPageSize){
        try {
            List<Map<String,Object>> mmTiwaiList = new ArrayList<>();
            List<Map<String,Object>> mmPinzhongAll = ddService.findPinzhongAll(ppxiangmuId,ppPageIndex,ppPageSize);
            for (int i = 0; i < mmPinzhongAll.size(); i++) {
                int mmPinzhongHebingshu = 0;
                Map<String,Object> mmMap = mmPinzhongAll.get(i);
                if (mmMap!=null){
                    String mmPinzhongOne = mmMap.get("pinzhong") == null ? "" : mmMap.get("pinzhong").toString();
                    List<Map<String,Object>> mmPinpailist = ddService.findPinpaiResultByPinzhong(ppxiangmuId,ppuserId,mmPinzhongOne);
                    if (mmPinpailist.size()>0){
                        mmPinzhongHebingshu = mmPinpailist.size();
                    }else{
                        mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                    }
                    mmMap.put("pinpailist",mmPinpailist);
                    mmMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmTiwaiList.add(mmMap);
            }
            Integer mmCount = ddService.getTiwaizhenduanlist(ppxiangmuId,ppPageIndex,ppPageSize);
            PageInfo mmPageInfo = new PageInfo(ppPageIndex, ppPageSize, mmCount);

            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("TiwaizhenduanList", mmTiwaiList);
            mmReturnMap.put("PageInfo", mmPageInfo);
            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("tiwaizhenduan/findtiwaizhenduanlist:error",e);
            return JsonResult.getErrorResult("tiwaizhenduan/findtiwaizhenduanlist:error " + e.getMessage());
        }


    }


    @RequestMapping("delete")
    public JsonResult<?> delete(@RequestParam("xiangmuid") String ppxiangmuId){
        try {

            return ddService.deleteByxiangmuId(ppxiangmuId)==true? JsonResult.getSuccessResult("删除材料成功")
                    : JsonResult.getErrorResult("删除材料失败");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("tiwaizhenduan/delete:error",e);
            return JsonResult.getErrorResult("tiwaizhenduan/delete:error " + e.getMessage());
        }
    }


}
